
    /********************************************************/
    /*                                                      */
    /*                                                      */
    /*         Copyright (C), 1993  SEIKO EPSON Corp.       */
    /*         ALL RIGHTS RESERVED                          */
    /*                                                      */
    /*                                                      */
    /*  file name : atoi.c                                  */
    /*                                                      */
    /*  Revision history                                    */
    /*      93/12/17    first release       S.Akaiwa        */
    /*      97/2/20	modified	    T.Katahira	    */
    /*			change function declaration	    */
    /*					to the ANSI style   */
    /*                                                      */
    /********************************************************/

#ifdef      UNIX
#include    "stdlib.h"
#include    "limits.h"
#include    "errno.h"
#else
#include    <stdlib.h>
#include    <limits.h>
#include    <errno.h>
#endif

int
atoi(const char * sStrPtrP)

    /********************************************************/
    /*                                                      */
    /*  The atoi function converts string pointed by        */
    /*  sStrPtrP into int.                                  */
    /*  The atoi function returns the converted value.      */
    /*  If nothing is converted, 0 is returns.              */
    /*  INT_MAX or INT_MIN is returned, and ERANGE is       */
    /*  stored in errno when overflow is generated.         */
    /*                                                      */
    /*  RETURN  INT_MAX/INT_MIN (errno == ERANGE)           */
    /*                  ... Overflow                        */
    /*          Others  ... Converted value                 */
    /*                                                      */
    /********************************************************/

    /* char            * sStrPtrP;          string pointer */ 
    {

     long           lRetVal;              /* converted data */  

                                  /* convert data into long */
     lRetVal = strtol(sStrPtrP, NULL, 10);

     if(lRetVal > (long)INT_MAX){         /* overflow check */
        errno = ERANGE;
        return  (int)INT_MAX;                   /* overflow */
     }
     else{
        if(lRetVal < (long)INT_MIN){    /* - overflow check */
           errno = ERANGE;
           return  (int)INT_MIN;              /* - overflow */
        }
        else{
           return  (int)lRetVal;   /* return converted data */
        }
     }
    }

